package com.snoob.serviceWorkOrder.workorder.controller;

import com.snoob.serviceWorkOrder.workorder.entity.WorkOrderEnterpriseEntity;
import com.snoob.serviceWorkOrder.workorder.entity.WorkOrderEntity;
import com.snoob.serviceWorkOrder.workorder.service.WorkOrderEnterpriseService;
import com.snoob.serviceWorkOrder.workorder.service.WorkOrderService;
import com.snoob.springcloudalibaba.base.BaseException;
import com.snoob.springcloudalibaba.pipleline.vo.CreateWorkOrderVoReq;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @描述：TODO 企业工单相关的抽象功能，对BFF层提供有关企业功能方法（1单一功能接口、2符合功能接口）
 * @作者：lixing_java@163.com
 * @时间：2022/5/14 16:16
 */
//@RestController
//@RequestMapping("/workOrder")
@Component
public class WorkOrderController {
    @Resource
    private WorkOrderService workOrderService;

    @Resource
    private WorkOrderEnterpriseService workOrderEnterpriseService;

    /**
     * 创建企业工单
     */
    public String workOrderCreate(CreateWorkOrderVoReq dto) {
        Long workOrderEnterpriseId = null;

        // 校验工单的子表信息（企业、企业用户）
        WorkOrderEnterpriseEntity workOrderEnterpriseEntity = new WorkOrderEnterpriseEntity();
        workOrderEnterpriseEntity.setEnterpriseCode(dto.getEnterpriseCode());
        workOrderEnterpriseEntity.setEnterpriseName(dto.getEnterpriseName());
        workOrderEnterpriseEntity.setEnterpriseSeq(dto.getEnterpriseSeq());
        workOrderEnterpriseEntity.setEnterpriseShowState(dto.getEnterpriseShowState());
        workOrderEnterpriseEntity.setUserAccount(dto.getUserAccount());
        workOrderEnterpriseEntity.setUserName(dto.getUserName());
        workOrderEnterpriseEntity.setUserPhone(dto.getUserPhone());
        workOrderEnterpriseEntity.setUserSeq(dto.getUserSeq());
        workOrderEnterpriseEntity.setUserShowState(dto.getUserShowState());
        WorkOrderEnterpriseEntity workOrderEnterpriseEntityForSelect = workOrderEnterpriseService.selectWorkOrderEnterprise(workOrderEnterpriseEntity);
        if (null == workOrderEnterpriseEntityForSelect) {
            // 添加工单的子表信息（企业、企业用户）
            workOrderEnterpriseId = workOrderEnterpriseService.insertWorkOrderEnterprise(workOrderEnterpriseEntity);
            if (null == workOrderEnterpriseId) {
                throw new BaseException("添加工单的子表信息失败");
            }
        }

        // 添加工单
        WorkOrderEntity workOrderEntity = new WorkOrderEntity();
        workOrderEntity.setWorkOrderEnterpriseId(workOrderEnterpriseId);
        workOrderEntity.setOrderName(dto.getOrderName());
        String orderCode = workOrderService.insertWorkOrder(workOrderEntity);
        if (null == orderCode) {
            throw new BaseException("工单创建失败");
        }

        return orderCode;
    }
}

